package org.cheetah.spring.xml;

import org.cheetah.activities.SetMsgIn;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

public class SetInDefinitionParser extends AbstractSingleBeanDefinitionParser {

	@Override
	protected Class<?> getBeanClass(Element element) {
		return SetMsgIn.class;
	}

	@Override
	protected void doParse(Element element, BeanDefinitionBuilder bean) {
		String preserveHeaders = element.getAttribute("preserve-headers");

		String value = element.getAttribute("value");
		String valueVar = element.getAttribute("value-var");
		String valueRef = element.getAttribute("value-ref");

		if ("true".equalsIgnoreCase(preserveHeaders)) {
			bean.addPropertyValue("preserveHeaders", Boolean.TRUE);
		} else {
			bean.addPropertyValue("preserveHeaders", Boolean.FALSE);
		}

		if (StringUtils.hasText(value)) {
			bean.addPropertyValue("value", value);
		}
		if (StringUtils.hasText(valueVar)) {
			bean.addPropertyValue("valueVar", valueVar);
		}
		if (StringUtils.hasText(valueRef)) {
			bean.addPropertyReference("valueRef", valueRef);
		}
	}
}
